﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Data;

//taken from http://heathesh.com/post/2010/05/10/A-simple-way-to-read-an-RSS-feed-using-C.aspx
namespace CoastalClimbing
{
     /// <summary>
    /// RSS feed item entity
    /// </summary>
    public class RssFeedItem
    {
        private int _shortTitleLength = 38;
        private string _title, _description, _link;        
        private DateTime _publishDate;

        /// <summary>
        /// Gets or sets the title
        /// </summary>
        public string Title 
        { 
            get { return _title; }
            set { _title = value; }
        }

        public string ShortTitle
        {
            get
            {
                if (_title.Length > _shortTitleLength)
                    return Title.Substring(0, _shortTitleLength - 1);
                else
                    return Title;
            }
        }
        
        /// <summary>
        /// Gets or sets the description
        /// </summary>
        public string Description
        {
            get { return _description; }
            set { _description = value; }
        }

        /// <summary>
        /// Gets or sets the link
        /// </summary>
        public string Link
        {
            get { return _link; }
            set { _link = value; }
        }
        
        /// <summary>
        /// Gets or sets the publish date
        /// </summary>
        public DateTime PublishDate
        {
            get { return _publishDate; }
            set { _publishDate = value; }
        }

    }

    /// <summary>
    /// RSS manager to read rss feeds
    /// </summary>
    public static class RssManager
    {
        private static int _rssCount = 4;

        /// <summary>
        /// Reads the relevant Rss feed and returns a list off RssFeedItems
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public static List<RssFeedItem> ReadFeed(string url)
        {
            //create a new list of the rss feed items to return
            List<RssFeedItem> rssFeedItems = new List<RssFeedItem>();

            //create an http request which will be used to retrieve the rss feed
            HttpWebRequest rssFeed = (HttpWebRequest)WebRequest.Create(url);
            rssFeed.Method = "GET";
            rssFeed.UserAgent = "Fiddler";

            //use a dataset to retrieve the rss feed
            using (DataSet rssData = new DataSet())
            {
                //read the xml from the stream of the web request                
                rssData.ReadXml(rssFeed.GetResponse().GetResponseStream());

                //loop through the rss items in the dataset and populate the list of rss feed items
                foreach (DataRow dataRow in rssData.Tables["item"].Rows)
                {
                    string date = dataRow["pubdate"].ToString();

                    rssFeedItems.Add(new RssFeedItem
                    {
                        Description = Convert.ToString(dataRow["description"]),
                        Link = Convert.ToString(dataRow["link"]),
                        Title = Convert.ToString(dataRow["title"]),
                        PublishDate = Convert.ToDateTime(String.Format("{0:MM/dd/yyyy hh:mm tt}", DateTime.Parse(date.Remove(date.LastIndexOf(" ")))))
                    });

                    if (rssFeedItems.Count == _rssCount)
                        break;
                }
            }

            //return the rss feed items
            return rssFeedItems;
        }
    }
}

